<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1"><meta name="format-detection" content="telephone=no"><meta name="apple-mobile-web-app-capable" content="yes"><meta name="apple-mobile-web-app-status-bar-style" content="black"><link rel="icon" href="/images/touxiang1.png?v=2.6.1" type="image/png" sizes="16x16"><link rel="icon" href="/images/touxiang1.png?v=2.6.1" type="image/png" sizes="32x32"><meta name="description" content="dasdas">
<meta property="og:type" content="website">
<meta property="og:title" content="Yangj&#39;s blog">
<meta property="og:url" content="https://gityang2016.gitee.io/index.html">
<meta property="og:site_name" content="Yangj&#39;s blog">
<meta property="og:description" content="dasdas">
<meta property="og:locale" content="en_US">
<meta property="article:author" content="Yangj">
<meta property="article:tag" content="vvasda">
<meta name="twitter:card" content="summary"><title>Yangj's blog</title><link ref="canonical" href="https://gityang2016.gitee.io/index.html"><link rel="dns-prefetch" href="https://cdn.jsdelivr.net"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.12.1/css/all.min.css" type="text/css"><link rel="stylesheet" href="/css/index.css?v=2.6.1"><script>var Stun = window.Stun || {};
var CONFIG = {
  root: '/',
  algolia: undefined,
  assistSearch: undefined,
  fontIcon: {"prompt":{"success":"fas fa-check-circle","info":"fas fa-arrow-circle-right","warning":"fas fa-exclamation-circle","error":"fas fa-times-circle"},"copyBtn":"fas fa-copy"},
  sidebar: {"offsetTop":"20px","tocMaxDepth":6},
  header: {"enable":true,"showOnPost":true,"scrollDownIcon":true},
  postWidget: {"endText":true},
  nightMode: {"enable":true},
  back2top: {"enable":true},
  codeblock: {"style":"default","highlight":"light","wordWrap":false},
  reward: false,
  fancybox: false,
  zoomImage: {"gapAside":"20px"},
  galleryWaterfall: undefined,
  lazyload: false,
  pjax: undefined,
  externalLink: {"icon":{"enable":true,"name":"fas fa-external-link-alt"}},
  shortcuts: undefined,
  prompt: {"copyButton":"Copy","copySuccess":"Copy Success","copyError":"Copy Error"},
  sourcePath: {"js":"js","css":"css","images":"images"},
};

window.CONFIG = CONFIG;</script><meta name="generator" content="Hexo 5.3.0"><link rel="alternate" href="/atom.xml" title="Yangj's blog" type="application/atom+xml">
</head><body><div class="container" id="container"><header class="header" id="header"><div class="header-inner"><nav class="header-nav header-nav--fixed"><div class="header-nav-inner"><div class="header-nav-menubtn"><i class="fas fa-bars"></i></div><div class="header-nav-menu"><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/"><span class="header-nav-menu-item__icon"><i class="fas fa-home"></i></span><span class="header-nav-menu-item__text">Home</span></a></div><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/archives/"><span class="header-nav-menu-item__icon"><i class="fas fa-folder-open"></i></span><span class="header-nav-menu-item__text">Archives</span></a></div><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/categories/"><span class="header-nav-menu-item__icon"><i class="fas fa-layer-group"></i></span><span class="header-nav-menu-item__text">Categories</span></a></div><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/tags/"><span class="header-nav-menu-item__icon"><i class="fas fa-tags"></i></span><span class="header-nav-menu-item__text">Tags</span></a></div><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/aboutme/"><span class="header-nav-menu-item__icon"><i class="fas fa-user-tag"></i></span><span class="header-nav-menu-item__text">About</span></a></div></div><div class="header-nav-mode"><div class="mode"><div class="mode-track"><span class="mode-track-moon"></span><span class="mode-track-sun"></span></div><div class="mode-thumb"></div></div></div></div></nav><div class="header-banner"><div class="header-banner-info"><div class="header-banner-info__title">Yangj's blog</div><div class="header-banner-info__subtitle">It is never too late to mend</div></div><div class="header-banner-arrow"><div class="header-banner-arrow__icon"><i class="fas fa-angle-down"></i></div></div></div></div></header><main class="main" id="main"><div class="main-inner"><div class="content-wrap" id="content-wrap"><div class="content content-home" id="content"><section class="postlist"><article class="postlist-item post"><header class="post-header"><h1 class="post-title"><a class="post-title__link" href="/2021/03/22/ngnix_01/">Nginx</a></h1><div class="post-meta"><span class="post-meta-item post-meta-item--createtime"><span class="post-meta-item__icon"><i class="far fa-calendar-plus"></i></span><span class="post-meta-item__info">Created</span><span class="post-meta-item__value">2021-03-22</span></span><span class="post-meta-item post-meta-item--updatetime"><span class="post-meta-item__icon"><i class="far fa-calendar-check"></i></span><span class="post-meta-item__info">Updated</span><span class="post-meta-item__value">2021-03-22</span></span></div></header><div class="post-body"><div class="post-excerpt">
        <h1 id="1-反向代理"   >
          <a href="#1-反向代理" class="heading-link"><i class="fas fa-link"></i></a>1.反向代理:</h1>
      <p><img src="/images/nginx/nginx1.png"> </p>
<p>反向代理:</p>
<p><img src="/images/nginx/nginx2.png"> </p>

        <h1 id="2-负载均衡"   >
          <a href="#2-负载均衡" class="heading-link"><i class="fas fa-link"></i></a>2.负载均衡</h1>
      <p><img src="/images/nginx/nginx3.png"> </p>

        <h1 id="3-动静分离"   >
          <a href="#3-动静分离" class="heading-link"><i class="fas fa-link"></i></a>3.动静分离</h1>
      <p><img src="/images/nginx/nginx4.png"> </p>
<p><img src="/images/nginx/nginx5.png"> </p>

        <h1 id="4-Nginx安装"   >
          <a href="#4-Nginx安装" class="heading-link"><i class="fas fa-link"></i></a>4.Nginx安装</h1>
      
        <h2 id="1-查看IP地址"   >
          <a href="#1-查看IP地址" class="heading-link"><i class="fas fa-link"></i></a>1.查看IP地址</h2>
      <p>使用xshell 或其他远程连接工具</p>
<figure class="highlight plain"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ifconfig</span><br></pre></td></tr></table></div></figure>
<p>安装编译工具,用root用户</p>
<figure class="highlight plain"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel</span><br></pre></td></tr></table></div></figure>

        <h2 id="2-pcre安装"   >
          <a href="#2-pcre安装" class="heading-link"><i class="fas fa-link"></i></a>2.pcre安装</h2>
      <p>下载地址:<br><span class="exturl"><a class="exturl__link"   target="_blank" rel="noopener" href="https://udomain.dl.sourceforge.net/project/pcre/pcre2/10.36/pcre2-10.36.zip" >https://udomain.dl.sourceforge.net/project/pcre/pcre2/10.36/pcre2-10.36.zip</a><span class="exturl__icon"><i class="fas fa-external-link-alt"></i></span></span><br>解压</p>
<figure class="highlight plain"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">unzip pcre2-10.36.zip</span><br></pre></td></tr></table></div></figure>
<p>进入pcre目录，执行 ./configure</p>
<figure class="highlight plain"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[root@yangj pcre2-10.36]$ .&#x2F;configure</span><br></pre></td></tr></table></div></figure>
<p>编译安装</p>
<figure class="highlight plain"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[root@yangj pcre2-10.36]$ make &amp;&amp; make install</span><br></pre></td></tr></table></div></figure>
<p>查看是否安装成功</p>
<figure class="highlight plain"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[root@yangj pcre2-10.36]# pcre-config --version</span><br></pre></td></tr></table></div></figure>

        <h2 id="3-nginx-安装"   >
          <a href="#3-nginx-安装" class="heading-link"><i class="fas fa-link"></i></a>3.nginx 安装</h2>
      <p>下载<br><span class="exturl"><a class="exturl__link"   target="_blank" rel="noopener" href="http://nginx.org/download/nginx-1.18.0.tar.gz" >nginx-1.18.0.tar.gz</a><span class="exturl__icon"><i class="fas fa-external-link-alt"></i></span></span><br>解压</p>
<figure class="highlight plain"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">tar -xvf nginx-1.18.0.tar.gz</span><br></pre></td></tr></table></div></figure>
<p>进入解压目录，执行./configure</p>
<figure class="highlight plain"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[root@yangj nginx-1.18.0]$ .&#x2F;configure</span><br></pre></td></tr></table></div></figure>
<p>编译安装</p>
<figure class="highlight plain"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[root@yangj nginx-1.18.0]$ make &amp;&amp; make all</span><br></pre></td></tr></table></div></figure>
<p>查看版本</p>
<figure class="highlight plain"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">[root@yangj sbin]# &#x2F;usr&#x2F;local&#x2F;nginx&#x2F;sbin&#x2F;nginx -v</span><br><span class="line">nginx version: nginx&#x2F;1.18.0</span><br></pre></td></tr></table></div></figure>
<p>查看防火墙命令:</p>
<figure class="highlight plain"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[root@yangj ~]# firewall-cmd --list-all</span><br></pre></td></tr></table></div></figure>
<p>开放80端口</p>
<figure class="highlight plain"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">[root@yangj ~]# firewall-cmd --add-service&#x3D;http --permanent</span><br><span class="line">[root@yangj ~]# firewall-cmd --add-port&#x3D;80&#x2F;tcp --permanent</span><br></pre></td></tr></table></div></figure>
<p>重启</p>
<figure class="highlight plain"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[root@yangj ~]# firewall-cmd --reload</span><br></pre></td></tr></table></div></figure>
<p>成功:</p>
<p><img src="/images/nginx/nginx6.png"> </p>
<p>页面访问<br><img src="/images/nginx/nginx7.png"> </p>

        <h1 id="5-Nginx反向代理示例一"   >
          <a href="#5-Nginx反向代理示例一" class="heading-link"><i class="fas fa-link"></i></a>5.Nginx反向代理示例一:</h1>
      <p>打开浏览器，在浏览器地址栏输入地址  tomcat.com，跳转到 liunx 系统 tomcat 主页面中。</p>

<p><span class="exturl"><a class="exturl__link"   target="_blank" rel="noopener" href="https://mirrors.bfsu.edu.cn/apache/tomcat/tomcat-8/v8.5.64/bin/apache-tomcat-8.5.64.tar.gz" >Tomcat下载</a><span class="exturl__icon"><i class="fas fa-external-link-alt"></i></span></span></p>
<p>新建文件夹，解压Tomcat安装包</p>
<figure class="highlight plain"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[yang@yangj tomcat8080]$ tar -xvf apache-tomcat-8.5.64.tar.gz</span><br></pre></td></tr></table></div></figure>
<p>启动Tomcat</p>
<figure class="highlight plain"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">[yang@yangj tomcat8080]$ cd apache-tomcat-8.5.64&#x2F;bin</span><br><span class="line">[yang@yangj bin]$ .&#x2F;startup.sh</span><br></pre></td></tr></table></div></figure>
<p>开放8080端口</p>
<figure class="highlight plain"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[root@yangj ~]# firewall-cmd --add-port&#x3D;8080&#x2F;tcp --permanent</span><br></pre></td></tr></table></div></figure>
<p>重启</p>
<figure class="highlight plain"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[root@yangj ~]# firewall-cmd --reload</span><br></pre></td></tr></table></div></figure>
<p>成功界面<br><img src="/images/nginx/nginx8.png"> </p>
<p>主机host,文件修改。把域名(如:tomcat.com)转发到对应IP(192.168.76.130)<br>C:\Windows\System32\drivers\etc\hosts 文件</p>
<figure class="highlight plain"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">192.168.76.130  tomcat.com</span><br></pre></td></tr></table></div></figure>
<p>修改nginx配置文件 vi /usr/local/nginx/conf/nginx.conf</p>
<p><img src="/images/nginx/nginx9.png"> </p>
<p>重启Nginx</p>
<figure class="highlight plain"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">cd &#x2F;usr&#x2F;local&#x2F;nginx&#x2F;sbin</span><br><span class="line">.&#x2F;nginx -s reload</span><br></pre></td></tr></table></div></figure>
<p>结果页:</p>
<p><img src="/images/nginx/nginx10.png"> </p>
<p>Tip： 通过nginx 把 tomcat.com 请求转发到  <span class="exturl"><a class="exturl__link"   target="_blank" rel="noopener" href="http://192.168.76.130:8080/" >http://192.168.76.130:8080</a><span class="exturl__icon"><i class="fas fa-external-link-alt"></i></span></span></p>

        <h1 id="6-Nginx反向代理示例二"   >
          <a href="#6-Nginx反向代理示例二" class="heading-link"><i class="fas fa-link"></i></a>6.Nginx反向代理示例二:</h1>
      <p>使用使用 nginx 反向代理，根据访问的路径跳转到不同端口的服务中(Tomcat) 
nginx 监听端口为 9001， 
访问  http://192.168.17.129:9001/edu/  直接跳转到 127.0.0.1:8080 
访问  http:// 192.168.17.129:9001/vod/  直接跳转到 127.0.0.1:8081 
</p>
配置8081,Tomcat
在一个新的文件夹解压Tomcat
apache-tomcat-8.5.64/conf/server.xml文件

<p><img src="/images/nginx/nginx11.png"> </p>
<p>启动Tomcat</p>
<p><img src="/images/nginx/nginx12.png"> </p>
<p>开放8081,9001端口</p>
<figure class="highlight plain"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">[root@yangj ~]# firewall-cmd --add-port&#x3D;8081&#x2F;tcp --permanent</span><br><span class="line">[root@yangj ~]# firewall-cmd --add-port&#x3D;9001&#x2F;tcp --permanent</span><br></pre></td></tr></table></div></figure>
<p>重启</p>
<figure class="highlight plain"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[root@yangj ~]# firewall-cmd --reload</span><br></pre></td></tr></table></div></figure>
<p>8081能访问</p>
<p><img src="/images/nginx/nginx13.png"> </p>
<p><strong>nginx配置</strong></p>
<figure class="highlight plain"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">server &#123;</span><br><span class="line">           listen 9001;</span><br><span class="line"></span><br><span class="line">           location ~&#x2F;edu&#x2F; &#123;</span><br><span class="line">                proxy_pass  http:&#x2F;&#x2F;127.0.0.1:8080;</span><br><span class="line">           &#125;    </span><br><span class="line">        </span><br><span class="line">           location ~&#x2F;vod&#x2F; &#123;</span><br><span class="line">                proxy_pass  http:&#x2F;&#x2F;127.0.0.1:8081;</span><br><span class="line">           &#125;</span><br><span class="line">        &#125;</span><br></pre></td></tr></table></div></figure>
<p><img src="/images/nginx/nginx16.png"> </p>
<p>重启Nginx</p>
<figure class="highlight plain"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">cd &#x2F;usr&#x2F;local&#x2F;nginx&#x2F;sbin</span><br><span class="line">.&#x2F;nginx -s reload</span><br></pre></td></tr></table></div></figure>
<p>创建测试文件</p>
<p><img src="/images/nginx/nginx14.png"> </p>
<p><img src="/images/nginx/nginx15.png"> </p>
<p><strong>测试结果:</strong></p>
<p><img src="/images/nginx/nginx17.png"> </p>
<p><img src="/images/nginx/nginx18.png"> </p>

        <h1 id="7-Nginx-配置实例-负载均衡"   >
          <a href="#7-Nginx-配置实例-负载均衡" class="heading-link"><i class="fas fa-link"></i></a>7.Nginx 配置实例-负载均衡</h1>
      <p>1、实现效果<br>（1）浏览器地址栏输入地址  <span class="exturl"><a class="exturl__link"   target="_blank" rel="noopener" href="http://192.168.76.130/edu/a.html%EF%BC%8C%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1%E6%95%88%E6%9E%9C%EF%BC%8C%E5%B9%B3%E5%9D%87" >http://192.168.76.130/edu/a.html，负载均衡效果，平均</a><span class="exturl__icon"><i class="fas fa-external-link-alt"></i></span></span> 到8080<br>和 8081 端口中 </p>
<p>2、准备工作<br>（1）准备两台 tomcat 服务器，一台 8080，一台 8081<br>（2）在两台 tomcat 里面 webapps 目录中，创建名称是 edu 文件夹，在 edu 文件夹中创建<br>页面 a.html，用于测试 </p>
<p>3、在 nginx 的配置文件中进行负载均衡的配置 </p>
<p><img src="http://gityang2016.gitee.io/my-images/nginx/nginx20.png"></p>
<p>结果:</p>
<p><img src="http://gityang2016.gitee.io/my-images/nginx/nginx21.png"></p>
<p><img src="http://gityang2016.gitee.io/my-images/nginx/nginx22.png"></p>
<p>4、nginx 分配服务器策略<br>第一种  轮询（默认）<br>每个请求按时间顺序逐一分配到不同的后端服务器，如果后端服务器 down 掉，能自动剔除。 </p>
<p>第二种 weight<br>weight 代表权重默认为 1,权重越高被分配的客户端越多 </p>
<p>第三种 ip_hash<br>每个请求按访问 ip 的 hash 结果分配，这样每个访客固定访问一个后端服务器 </p>
<p>第四种 fair（第三方）<br>按后端服务器的响应时间来分配请求，响应时间短的优先分配。 </p>

        <h1 id="8-Nginx-配置实例-动静分离"   >
          <a href="#8-Nginx-配置实例-动静分离" class="heading-link"><i class="fas fa-link"></i></a>8.Nginx 配置实例-动静分离</h1>
      <p>1、什么是动静分离 </p>
<p><img src="http://gityang2016.gitee.io/my-images/nginx/nginx23.png"></p>
<p>通过  location  指定不同的后缀名实现不同的请求转发。通过  expires  参数设置，可以使浏<br>览器缓存过期时间，减少与服务器之前的请求和流量。具体  Expires  定义：是给一个资源<br>设定一个过期时间，也就是说无需去服务端验证，直接通过浏览器自身确认是否过期即可，<br>所以不会产生额外的流量。此种方法非常适合不经常变动的资源。（如果经常更新的文件，<br>不建议使用  Expires  来缓存），我这里设置  3d，表示在这  3  天之内访问这个  URL，发送一<br>个请求，比对服务器该文件最后更新时间没有变化，则不会从服务器抓取，返回状态码  304，<br>如果有修改，则直接从服务器重新下载，返回状态码  200。 </p>
<p>2、准备工作<br>（1）在 liunx 系统中准备静态资源，用于进行访问 </p>
<p><img src="http://gityang2016.gitee.io/my-images/nginx/nginx24.png"></p>
<p>3、具体配置<br>（1）在 nginx 配置文件中进行配置 </p>
<p><img src="http://gityang2016.gitee.io/my-images/nginx/nginx25.jpg"></p>
<p>4、最终测试<br>（1）浏览器中输入地址<br><span class="exturl"><a class="exturl__link"   target="_blank" rel="noopener" href="http://192.168.76.130/images/1.jpg" >http://192.168.76.130/images/1.jpg</a><span class="exturl__icon"><i class="fas fa-external-link-alt"></i></span></span></p>
<p><img src="http://gityang2016.gitee.io/my-images/nginx/nginx26.jpg"></p>
<p><img src="http://gityang2016.gitee.io/my-images/nginx/nginx27.jpg"></p>
<p>（2）在浏览器地址栏输入地址<br><span class="exturl"><a class="exturl__link"   target="_blank" rel="noopener" href="http://192.168.76.130/www/a.html" >http://192.168.76.130/www/a.html</a><span class="exturl__icon"><i class="fas fa-external-link-alt"></i></span></span></p>
<p><img src="http://gityang2016.gitee.io/my-images/nginx/nginx28.jpg"></p>

        <h1 id="9-Nginx-配置高可用的集群"   >
          <a href="#9-Nginx-配置高可用的集群" class="heading-link"><i class="fas fa-link"></i></a>9.Nginx 配置高可用的集群</h1>
      <p>1、什么是 nginx 高可用 </p>
<p><img src="http://gityang2016.gitee.io/my-images/nginx/nginx29.jpg"></p>
<p>2、配置高可用的准备工作<br>（1）需要两台服务器  192.168.76.130  和  192.168.76.131<br>（2）在两台服务器安装 nginx<br>（3）在两台服务器安装 keepalived </p>
<p><img src="http://gityang2016.gitee.io/my-images/nginx/nginx30.png"></p>
<p>3、在两台服务器安装 keepalived s<br>（1）使用 yum 命令进行安装 </p>
<figure class="highlight plain"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yum install keepalived –y </span><br></pre></td></tr></table></div></figure>
<p>（2）安装之后，在 etc 里面生成目录 keepalived，有文件 keepalived.conf </p>
<figure class="highlight plain"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">systemctl start keepalived.service</span><br></pre></td></tr></table></div></figure>
<p>4、完成高可用配置（主从配置）<br>（1）修改/etc/keepalived/keepalivec.conf 配置文件 </p>
<figure class="highlight plain"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line">global_defs &#123; </span><br><span class="line">      notification_email &#123; </span><br><span class="line">          acassen@firewall.loc </span><br><span class="line">          failover@firewall.loc </span><br><span class="line">          sysadmin@firewall.loc </span><br><span class="line">      &#125; </span><br><span class="line">      notification_email_from Alexandre.Cassen@firewall.loc </span><br><span class="line">      smtp_server 192.168.76.131   #ip地址</span><br><span class="line">      smtp_connect_timeout 30 </span><br><span class="line">      router_id LVS_DEVEL </span><br><span class="line">&#125; </span><br><span class="line"></span><br><span class="line">vrrp_script chk_http_port &#123; </span><br><span class="line"></span><br><span class="line">      script &quot;&#x2F;usr&#x2F;local&#x2F;src&#x2F;nginx_check.sh&quot; </span><br><span class="line">       </span><br><span class="line">      interval 2            #（检测脚本执行的间隔） </span><br><span class="line">       </span><br><span class="line">      weight 2 </span><br><span class="line"></span><br><span class="line">&#125; </span><br><span class="line"></span><br><span class="line">vrrp_instance VI_1 &#123; </span><br><span class="line">        state BACKUP      #  备份服务器上将  MASTER  改为  BACKUP     </span><br><span class="line">        interface ens33    &#x2F;&#x2F;网卡 </span><br><span class="line">        virtual_router_id 51      #  主、备机的 virtual_router_id 必须相同 </span><br><span class="line">        priority 80          #  主、备机取不同的优先级，主机值较大，备份机值较小 </span><br><span class="line">        advert_int 1 </span><br><span class="line">	authentication &#123; </span><br><span class="line">	auth_type PASS </span><br><span class="line">	auth_pass 1111 </span><br><span class="line">        &#125; </span><br><span class="line">        virtual_ipaddress &#123; </span><br><span class="line">                192.168.76.50 &#x2F;&#x2F; VRRP H 虚拟地址 </span><br><span class="line">        &#125;</span><br><span class="line">	track_script &#123;</span><br><span class="line">         chk_http_port</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">&#125; </span><br></pre></td></tr></table></div></figure>
<p>（2）在/usr/local/src 添加检测脚本 </p>
<p>/usr/local/src/nginx_check.sh,注意编码格式:Unix(LF)</p>
<figure class="highlight plain"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">A&#x3D;&#96;ps -C nginx --no-header |wc -l&#96;</span><br><span class="line">if [ $A -eq 0 ];then</span><br><span class="line">    &#x2F;usr&#x2F;local&#x2F;nginx&#x2F;sbin&#x2F;nginx</span><br><span class="line">    sleep 2</span><br><span class="line">    if [ &#96;ps -C nginx --no-header |wc -l&#96; -eq 0 ];then</span><br><span class="line">        killall keepalived</span><br><span class="line">    fi</span><br><span class="line">fi</span><br></pre></td></tr></table></div></figure>
<p>（3）把两台服务器上 nginx 和 keepalived 启动<br>启动 nginx：./nginx<br>启动 keepalived：systemctl start keepalived.service </p>
<p>5、最终测试<br>（1）在浏览器地址栏输入  虚拟 ip 地址 <span class="exturl"><a class="exturl__link"   target="_blank" rel="noopener" href="http://192.168.76.50/" >http://192.168.76.50/</a><span class="exturl__icon"><i class="fas fa-external-link-alt"></i></span></span>   </p>
<p>（2）把主服务器（192.168.76.130）nginx 和 keepalived 停止，再输入 192.168.76.50 </p>
<figure class="highlight plain"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">systemctl stop keepalived.service </span><br></pre></td></tr></table></div></figure>
<p>/usr/local/nginx/sbin</p>
<figure class="highlight plain"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">.&#x2F;nginx -s stop</span><br></pre></td></tr></table></div></figure>
<p><img src="http://gityang2016.gitee.io/my-images/nginx/nginx31.png"></p>

        <h1 id="10-Nginx-的原理"   >
          <a href="#10-Nginx-的原理" class="heading-link"><i class="fas fa-link"></i></a>10.Nginx 的原理</h1>
      <p>1、mater 和 worker </p>
<p><img src="http://gityang2016.gitee.io/my-images/nginx/nginx34.jpg"></p>
<p><img src="http://gityang2016.gitee.io/my-images/nginx/nginx32.jpg"></p>
<p>2、worker 如何进行工作的 </p>
<p><img src="http://gityang2016.gitee.io/my-images/nginx/nginx33.jpg"></p>
<p>3、一个 master 和多个 woker 有好处<br>（1）可以使用 nginx –s reload  热部署，利用 nginx 进行热部署操作<br>（2）每个 woker 是独立的进程，如果有其中的一个 woker 出现问题，其他 woker 独立的，<br>继续进行争抢，实现请求过程，不会造成服务中断 </p>
<p>4、设置多少个 woker 合适 </p>
<p>worker 数和服务器的 cpu 数相等是最为适宜的 </p>
<p>5、连接数 worker_connection<br>第一个：发送请求，占用了 woker 的几个连接数？<br>答案：2 或者 4 个 </p>
<p>第二个：nginx 有一个 master，有四个 woker，每个 woker 支持最大的连接数 1024，支持的<br>最大并发数是多少？<br>  普通的静态访问最大并发数是：  worker_connections * worker_processes /2，<br>  而如果是 HTTP 作  为反向代理来说，最大并发数量应该是 worker_connections *<br>worker_processes/4。 </p>
</div></div></article><article class="postlist-item post"><header class="post-header"><h1 class="post-title"><a class="post-title__link" href="/2021/01/27/java_annotation_01/">反射</a></h1><div class="post-meta"><span class="post-meta-item post-meta-item--createtime"><span class="post-meta-item__icon"><i class="far fa-calendar-plus"></i></span><span class="post-meta-item__info">Created</span><span class="post-meta-item__value">2021-01-27</span></span><span class="post-meta-item post-meta-item--updatetime"><span class="post-meta-item__icon"><i class="far fa-calendar-check"></i></span><span class="post-meta-item__info">Updated</span><span class="post-meta-item__value">2021-02-03</span></span></div></header><div class="post-body"><div class="post-excerpt">
        <h1 id="反射-01"   >
          <a href="#反射-01" class="heading-link"><i class="fas fa-link"></i></a>反射_01</h1>
      
        <h2 id="创建运行时类的对象"   >
          <a href="#创建运行时类的对象" class="heading-link"><i class="fas fa-link"></i></a>创建运行时类的对象</h2>
      
        <h3 id="有了Class对象，能做什么？"   >
          <a href="#有了Class对象，能做什么？" class="heading-link"><i class="fas fa-link"></i></a>有了Class对象，能做什么？</h3>
      <p>创建类的对象： 调用Class对象的newInstance()方法<br><br>要 求： 1）类必须有一个无参数的构造器。<br>2）类的构造器的访问权限需要足够。<br><br>难道没有无参的构造器就不能创建对象了吗？<br><br>不是！只要在操作的时候明确的调用类中的构造器， 并将参数传递进去之后，才可以实例化操作。<br><br>步骤如下：<br>1）通过Class类的getDeclaredConstructor(Class … parameterTypes)取得本类的指定形参类型的构造器<br><br>2）向构造器的形参中传递一个对象数组进去，里面包含了构造器中所需的各个参数。<br><br>3）通过Constructor实例化对象。</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//1.根据全类名获取对应的Class对象</span></span><br><span class="line">String name = “atguigu.java.Person<span class="string">&quot;;</span></span><br><span class="line"><span class="string">Class clazz = null;</span></span><br><span class="line"><span class="string">clazz = Class.forName(name);</span></span><br><span class="line"><span class="string">//2.调用指定参数结构的构造器，生成Constructor的实例</span></span><br><span class="line"><span class="string">Constructor con = clazz.getConstructor(String.class,Integer.class);</span></span><br><span class="line"><span class="string">//3.通过Constructor的实例创建对应类的对象，并初始化类属性</span></span><br><span class="line"><span class="string">Person p2 = (Person) con.newInstance(&quot;</span>Peter<span class="string">&quot;,20);</span></span><br><span class="line"><span class="string">System.out.println(p2);</span></span><br></pre></td></tr></table></div></figure>

        <h2 id="通过反射获取运行时类的完整结构"   >
          <a href="#通过反射获取运行时类的完整结构" class="heading-link"><i class="fas fa-link"></i></a>通过反射获取运行时类的完整结构</h2>
      <p>Field、 Method、 Constructor、 Superclass、 Interface、 Annotation<br><br> 实现的全部接口<br><br> 所继承的父类<br><br>全部的构造器<br><br> 全部的方法<br><br> 全部的Field</p>
<p>使用反射可以取得：<br><br>1.实现的全部接口<br><br> public Class&lt;?&gt;[] getInterfaces()<br>确定此对象所表示的类或接口实现的接口。<br><br>2.所继承的父类<br><br> public Class&lt;? Super T&gt; getSuperclass()<br>返回表示此 Class 所表示的实体（类、接口、基本类型）的父类的Class。</p>
<p>3.全部的构造器<br><br> public Constructor<T>[] getConstructors()<br><br>返回此 Class 对象所表示的类的所有public构造方法。<br><br>public Constructor<T>[] getDeclaredConstructors()<br><br>返回此 Class 对象表示的类声明的所有构造方法。<br> <br>Constructor类中：<br> <br>取得修饰符: public int getModifiers();<br> <br>取得方法名称: public String getName();<br> <br>取得参数的类型： public Class&lt;?&gt;[] getParameterTypes();</p>
<p>4.全部的方法<br><br> public Method[] getDeclaredMethods()<br><br>返回此Class对象所表示的类或接口的全部方法<br><br> public Method[] getMethods()<br><br>返回此Class对象所表示的类或接口的public的方法<br><br> Method类中：<br><br> public Class<?> getReturnType()取得全部的返回值
<br>public Class<?>[] getParameterTypes()取得全部的参数<br><br> public int getModifiers()取得修饰符<br><br> public Class&lt;?&gt;[] getExceptionTypes()取得异常信息</p>
<p>4.全部的方法<br><br> public Method[] getDeclaredMethods()<br>返回此Class对象所表示的类或接口的全部方法<br><br> public Method[] getMethods()<br>返回此Class对象所表示的类或接口的public的方法<br><br> Method类中：<br><br> public Class<?> getReturnType()取得全部的返回值
<br> public Class<?>[] getParameterTypes()取得全部的参数<br><br> public int getModifiers()取得修饰符<br><br> public Class&lt;?&gt;[] getExceptionTypes()取得异常信息<br><br>6. Annotation相关<br><br> get Annotation(Class<T> annotationClass)<br><br> getDeclaredAnnotations()<br><br>7.泛型相关<br><br>获取父类泛型类型： Type getGenericSuperclass()<br><br>泛型类型： ParameterizedType<br><br>获取实际的泛型类型参数数组： getActualTypeArguments()<br><br>8.类所在的包 Package getPackage()</p>

        <h2 id="调用运行时类的指定结构"   >
          <a href="#调用运行时类的指定结构" class="heading-link"><i class="fas fa-link"></i></a>调用运行时类的指定结构</h2>
      
        <h3 id="1-调用指定方法"   >
          <a href="#1-调用指定方法" class="heading-link"><i class="fas fa-link"></i></a>1.调用指定方法</h3>
      <p>通过反射，调用类中的方法，通过Method类完成。步骤：<br><br>1.通过Class类的getMethod(String name,Class…parameterTypes)方法取得<br>一个Method对象，并设置此方法操作时所需要的参数类型。<br><br>2.之后使用Object invoke(Object obj, Object[] args)进行调用，并向方法中<br>传递要设置的obj对象的参数信息。</p>
<p><br>Object invoke(Object obj, Object … args)<br><br>说明：<br><br>1.Object 对应原方法的返回值，若原方法无返回值，此时返回null<br><br>2.若原方法若为静态方法，此时形参Object obj可为null<br><br>3.若原方法形参列表为空，则Object[] args为null<br><br>4.若原方法声明为private,则需要在调用此invoke()方法前，显式调用<br>方法对象的setAccessible(true)方法，将可访问private的方法。</p>
<p>2.调用指定属性<br><br>在反射机制中，可以直接通过Field类操作类中的属性，通过Field类提供的set()和<br>get()方法就可以完成设置和取得属性内容的操作。<br><br> public Field getField(String name) 返回此Class对象表示的类或接口的指定的<br>public的Field。<br><br> public Field getDeclaredField(String name)返回此Class对象表示的类或接口的<br>指定的Field。<br><br>在Field中：<br><br> public Object get(Object obj) 取得指定对象obj上此Field的属性内容<br><br> public void set(Object obj,Object value) 设置指定对象obj上此Field的属性内容</p>

        <h2 id="反射的应用：动态代理"   >
          <a href="#反射的应用：动态代理" class="heading-link"><i class="fas fa-link"></i></a>反射的应用：动态代理</h2>
      <p><br>代理设计模式的原理:<br>使用一个代理将对象包装起来, 然后用该代理对象取代原始对象。任何对原<br>始对象的调用都要通过代理。代理对象决定是否以及何时将方法调用转到原<br>始对象上。<br><br> 之前为大家讲解过代理机制的操作，属于静态代理，特征是代理类和目标<br>对象的类都是在编译期间确定下来，不利于程序的扩展。同时，每一个代<br>理类只能为一个接口服务，这样一来程序开发中必然产生过多的代理。 最<br>好可以通过一个代理类完成全部的代理功能。</p>
<p><br>动态代理是指客户通过代理类来调用其它对象的方法，并且是在程序运行时<br>根据需要动态创建目标类的代理对象。<br><br>动态代理使用场合:<br><br>调试<br><br>远程方法调用<br><br>动态代理相比于静态代理的优点：<br>抽象角色中（接口）声明的所有方法都被转移到调用处理器一个集中的方法中<br>处理，这样，我们可以更加灵活和统一的处理众多的方法。<br><img src="/images/java/reflect/reflect10.png"><br><img src="/images/java/reflect/reflect11.png"><br><img src="/images/java/reflect/reflect12.png"><br><img src="/images/java/reflect/reflect13.png"></p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">RealSubject target = <span class="keyword">new</span> RealSubject();</span><br><span class="line"><span class="comment">// Create a proxy to wrap the original implementation</span></span><br><span class="line">DebugProxy proxy = <span class="keyword">new</span> DebugProxy(target);</span><br><span class="line"><span class="comment">// Get a reference to the proxy through the Subject interface</span></span><br><span class="line">Subject sub = (Subject) Proxy.newProxyInstance(</span><br><span class="line">Subject.class.getClassLoader(),<span class="keyword">new</span> Class[] &#123; Subject.class &#125;, proxy);</span><br><span class="line"></span><br><span class="line"><span class="number">4.</span>通过 Subject代理调用RealSubject实现类的方法</span><br><span class="line">String info = sub.say(“Peter<span class="string">&quot;, 24);</span></span><br><span class="line"><span class="string">System.out.println(info);</span></span><br></pre></td></tr></table></div></figure>

        <h2 id="动态代理与AOP（Aspect-Orient-Programming"   >
          <a href="#动态代理与AOP（Aspect-Orient-Programming" class="heading-link"><i class="fas fa-link"></i></a>动态代理与AOP（Aspect Orient Programming)</h2>
      <p><img src="/images/java/reflect/reflect18.png"><br> <br>使用Proxy生成一个动态代理时，往往并不会凭空产生一个动态代理，这样没有<br>太大的意义。通常都是为指定的目标对象生成动态代理<br> <br>这种动态代理在AOP中被称为AOP代理， AOP代理可代替目标对象， AOP代理<br>包含了目标对象的全部方法。但AOP代理中的方法与目标对象的方法存在差异：<br>AOP代理里的方法可以在执行目标方法之前、之后插入一些通用处理</p>
</div></div></article><article class="postlist-item post"><header class="post-header"><h1 class="post-title"><a class="post-title__link" href="/2021/01/27/JVM_01/">JVM</a></h1><div class="post-meta"><span class="post-meta-item post-meta-item--createtime"><span class="post-meta-item__icon"><i class="far fa-calendar-plus"></i></span><span class="post-meta-item__info">Created</span><span class="post-meta-item__value">2021-01-27</span></span><span class="post-meta-item post-meta-item--updatetime"><span class="post-meta-item__icon"><i class="far fa-calendar-check"></i></span><span class="post-meta-item__info">Updated</span><span class="post-meta-item__value">2021-01-27</span></span></div></header><div class="post-body"><div class="post-excerpt">
        <h1 id="JVM-01"   >
          <a href="#JVM-01" class="heading-link"><i class="fas fa-link"></i></a>JVM_01</h1>
      
        <h2 id="学习JVM"   >
          <a href="#学习JVM" class="heading-link"><i class="fas fa-link"></i></a>学习JVM</h2>
      <p><img src="/images/jvm/jvm1.png"> </p>

        <h2 id="Java语言、JVM"   >
          <a href="#Java语言、JVM" class="heading-link"><i class="fas fa-link"></i></a>Java语言、JVM</h2>
      <p><img src="/images/jvm/jvm2.png"></p>
<p><img src="/images/jvm/jvm3.png"><br><img src="/images/jvm/jvm4.png"></p>

        <h3 id="JVM工作流程"   >
          <a href="#JVM工作流程" class="heading-link"><i class="fas fa-link"></i></a>JVM工作流程</h3>
      <p><img src="/images/jvm/jvm5.png"><br><img src="/images/jvm/jvm6.png"><br><img src="/images/jvm/jvm7.png"></p>

        <h2 id="JVM生命周期"   >
          <a href="#JVM生命周期" class="heading-link"><i class="fas fa-link"></i></a>JVM生命周期</h2>
      <p><img src="/images/jvm/jvm8.png"><br><img src="/images/jvm/jvm9.png"><br><img src="/images/jvm/jvm10.png"></p>

        <h2 id="JVM内存简图"   >
          <a href="#JVM内存简图" class="heading-link"><i class="fas fa-link"></i></a>JVM内存简图</h2>
      <p><img src="/images/jvm/jvm11.png"><br><img src="/images/jvm/jvm12.png"><br><img src="/images/jvm/jvm13.png"><br><img src="/images/jvm/jvm14.png"><br><img src="/images/jvm/jvm15.png"><br><img src="/images/jvm/jvm16.png"><br><img src="/images/jvm/jvm17.png"><br><img src="/images/jvm/jvm18.png"><br><img src="/images/jvm/jvm19.png"><br><img src="/images/jvm/jvm20.png"></p>

        <h2 id="双亲委派机制"   >
          <a href="#双亲委派机制" class="heading-link"><i class="fas fa-link"></i></a>双亲委派机制</h2>
      <p><img src="/images/jvm/jvm24.png"><br><img src="/images/jvm/jvm25.png"><br><img src="/images/jvm/jvm26.png"><br><img src="/images/jvm/jvm27.png"></p>
</div></div></article><article class="postlist-item post"><header class="post-header"><h1 class="post-title"><a class="post-title__link" href="/2021/01/27/mysql_01/">MySQL_01</a></h1><div class="post-meta"><span class="post-meta-item post-meta-item--createtime"><span class="post-meta-item__icon"><i class="far fa-calendar-plus"></i></span><span class="post-meta-item__info">Created</span><span class="post-meta-item__value">2021-01-27</span></span><span class="post-meta-item post-meta-item--updatetime"><span class="post-meta-item__icon"><i class="far fa-calendar-check"></i></span><span class="post-meta-item__info">Updated</span><span class="post-meta-item__value">2021-01-27</span></span></div></header><div class="post-body"><div class="post-excerpt">
        <h1 id="Mysql"   >
          <a href="#Mysql" class="heading-link"><i class="fas fa-link"></i></a>Mysql</h1>
      
        <h2 id="DB"   >
          <a href="#DB" class="heading-link"><i class="fas fa-link"></i></a>DB</h2>
      <p>数据库（database）：存储数据的“仓库”。它保存了一系列有组织的数据。</p>

        <h2 id="DBMS"   >
          <a href="#DBMS" class="heading-link"><i class="fas fa-link"></i></a>DBMS</h2>
      <p>数据库管理系统（Database Management System）。数据库是通过DBMS创建和操作的容器</p>

        <h2 id="SQL"   >
          <a href="#SQL" class="heading-link"><i class="fas fa-link"></i></a>SQL</h2>
      <p>结构化查询语言（Structure Query Language）：专门用来与数据库通信的语言。</p>

        <h2 id="MySQL产品的介绍和安装"   >
          <a href="#MySQL产品的介绍和安装" class="heading-link"><i class="fas fa-link"></i></a>MySQL产品的介绍和安装</h2>
      
        <h3 id="MySQL服务的启动和停止"   >
          <a href="#MySQL服务的启动和停止" class="heading-link"><i class="fas fa-link"></i></a>MySQL服务的启动和停止</h3>
      <pre><code>方式一：计算机——右击管理——服务
方式二：通过管理员身份运行
net start 服务名（启动服务）
net stop 服务名（停止服务）</code></pre>

        <h3 id="MySQL服务的登录和退出"   >
          <a href="#MySQL服务的登录和退出" class="heading-link"><i class="fas fa-link"></i></a>MySQL服务的登录和退出</h3>
      <pre><code>方式一：通过mysql自带的客户端
只限于root用户

方式二：通过windows自带的客户端
登录：
mysql 【-h主机名 -P端口号 】-u用户名 -p密码

退出：
exit或ctrl+C</code></pre>

        <h2 id="MySQL的常见命令"   >
          <a href="#MySQL的常见命令" class="heading-link"><i class="fas fa-link"></i></a>MySQL的常见命令</h2>
      <pre><code>1.查看当前所有的数据库
show databases;
2.打开指定的库
use 库名
3.查看当前库的所有表
show tables;
4.查看其它库的所有表
show tables from 库名;
5.创建表
create table 表名(

    列名 列类型,
    列名 列类型，
    。。。
);
6.查看表结构
desc 表名;

7.查看服务器的版本
方式一：登录到mysql服务端
select version();
方式二：没有登录到mysql服务端
mysql --version
或
mysql -V</code></pre>

        <h2 id="SQL的常见命令"   >
          <a href="#SQL的常见命令" class="heading-link"><i class="fas fa-link"></i></a>SQL的常见命令</h2>
      <pre><code>show databases； 查看所有的数据库
use 库名； 打开指定 的库
show tables ; 显示库中的所有表
show tables from 库名;显示指定库中的所有表
create table 表名(
    字段名 字段类型,    
    字段名 字段类型
); 创建表

desc 表名; 查看指定表的结构
select * from 表名;显示表中的所有数据</code></pre>

        <h2 id="进阶1：基础查询"   >
          <a href="#进阶1：基础查询" class="heading-link"><i class="fas fa-link"></i></a>进阶1：基础查询</h2>
      <p>语法：<br>select 查询列表 from 表名;</p>
<p>USE myemployees;</p>

        <h3 id="1-查询表中的单个字段"   >
          <a href="#1-查询表中的单个字段" class="heading-link"><i class="fas fa-link"></i></a>1.查询表中的单个字段</h3>
      <p>SELECT last_name FROM employees;</p>

        <h3 id="2-查询表中的多个字段"   >
          <a href="#2-查询表中的多个字段" class="heading-link"><i class="fas fa-link"></i></a>2.查询表中的多个字段</h3>
      <p>SELECT last_name,salary,email FROM employees;</p>

        <h3 id="3-查询表中的所有字段"   >
          <a href="#3-查询表中的所有字段" class="heading-link"><i class="fas fa-link"></i></a>3.查询表中的所有字段</h3>
      <p>方式一：</p>
<figure class="highlight sql"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> </span><br><span class="line">    `employee_id`,</span><br><span class="line">    `first_name`,</span><br><span class="line">    `last_name`,</span><br><span class="line">    `phone_number`,</span><br><span class="line">    `last_name`,</span><br><span class="line">    `job_id`,</span><br><span class="line">    `phone_number`,</span><br><span class="line">    `job_id`,</span><br><span class="line">    `salary`,</span><br><span class="line">    `commission_pct`,</span><br><span class="line">    `manager_id`,</span><br><span class="line">    `department_id`,</span><br><span class="line">    `hiredate` </span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line">    employees ;</span><br></pre></td></tr></table></div></figure>
<p> 方式二：<br> SELECT * FROM employees;</p>

        <h3 id="4-查询常量值"   >
          <a href="#4-查询常量值" class="heading-link"><i class="fas fa-link"></i></a>4.查询常量值</h3>
      <p> SELECT 100;<br> SELECT ‘john’;</p>

        <h3 id="5-查询表达式"   >
          <a href="#5-查询表达式" class="heading-link"><i class="fas fa-link"></i></a>5.查询表达式</h3>
      <p> SELECT 100%98;</p>

        <h3 id="6-查询函数"   >
          <a href="#6-查询函数" class="heading-link"><i class="fas fa-link"></i></a>6.查询函数</h3>
      <p> SELECT VERSION();</p>

        <h3 id="7-起别名"   >
          <a href="#7-起别名" class="heading-link"><i class="fas fa-link"></i></a>7.起别名</h3>
       <figure class="highlight plain"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">①便于理解</span><br><span class="line">②如果要查询的字段有重名的情况，使用别名可以区分开来</span><br></pre></td></tr></table></div></figure>
<p> 方式一：使用as<br> <figure class="highlight sql"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> <span class="number">100</span><span class="operator">%</span><span class="number">98</span> <span class="keyword">AS</span> 结果;</span><br><span class="line"><span class="keyword">SELECT</span> last_name <span class="keyword">AS</span> 姓,first_name <span class="keyword">AS</span> 名 <span class="keyword">FROM</span> employees;</span><br></pre></td></tr></table></div></figure><br>方式二：使用空格<br> <figure class="highlight sql"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> last_name 姓,first_name 名 <span class="keyword">FROM</span> employees;</span><br></pre></td></tr></table></div></figure><br>#案例：查询salary，显示结果为 out put<br> <figure class="highlight sql"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> salary <span class="keyword">AS</span> &quot;out put&quot; <span class="keyword">FROM</span> employees;</span><br></pre></td></tr></table></div></figure></p>

        <h3 id="8-去重"   >
          <a href="#8-去重" class="heading-link"><i class="fas fa-link"></i></a>8.去重</h3>
      <p>案例：查询员工表中涉及到的所有的部门编号<br> <figure class="highlight sql"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> <span class="keyword">DISTINCT</span> department_id <span class="keyword">FROM</span> employees;</span><br></pre></td></tr></table></div></figure></p>

        <h3 id="9-号的作用"   >
          <a href="#9-号的作用" class="heading-link"><i class="fas fa-link"></i></a>9.+号的作用</h3>
      <p>/*</p>
<p>java中的+号：<br>①运算符，两个操作数都为数值型<br>②连接符，只要有一个操作数为字符串</p>
<p>mysql中的+号：<br>仅仅只有一个功能：运算符</p>
<p>select 100+90; 两个操作数都为数值型，则做加法运算<br>select ‘123’+90;只要其中一方为字符型，试图将字符型数值转换成数值型。如果转换成功，则继续做加法运算<br>select ‘john’+90;    如果转换失败，则将字符型数值转换成0</p>
<p>select null+10; 只要其中一方为null，则结果肯定为null</p>
<p>*/</p>
<p>案例：查询员工名和姓连接成一个字段，并显示为 姓名</p>
 <figure class="highlight sql"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> CONCAT(<span class="string">&#x27;a&#x27;</span>,<span class="string">&#x27;b&#x27;</span>,<span class="string">&#x27;c&#x27;</span>) <span class="keyword">AS</span> 结果;</span><br><span class="line"></span><br><span class="line"><span class="keyword">SELECT</span> </span><br><span class="line">	CONCAT(last_name,first_name) <span class="keyword">AS</span> 姓名</span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line">	employees;</span><br></pre></td></tr></table></div></figure>

        <h2 id="进阶2：条件查询"   >
          <a href="#进阶2：条件查询" class="heading-link"><i class="fas fa-link"></i></a>进阶2：条件查询</h2>
      <p>/*</p>
<p>语法：<br>    select<br>        查询列表<br>    from<br>        表名<br>    where<br>        筛选条件;</p>
<p>分类：</p>

        <h3 id="一、按条件表达式筛选"   >
          <a href="#一、按条件表达式筛选" class="heading-link"><i class="fas fa-link"></i></a>一、按条件表达式筛选</h3>
      <pre><code>简单条件运算符：&gt; &lt; = != &lt;&gt; &gt;= &lt;=</code></pre>

        <h3 id="二、按逻辑表达式筛选"   >
          <a href="#二、按逻辑表达式筛选" class="heading-link"><i class="fas fa-link"></i></a>二、按逻辑表达式筛选</h3>
      <pre><code>逻辑运算符：
作用：用于连接条件表达式
    &amp;&amp; || !
    and or not

&amp;&amp;和and：两个条件都为true，结果为true，反之为false
||或or： 只要有一个条件为true，结果为true，反之为false
!或not： 如果连接的条件本身为false，结果为true，反之为false</code></pre>

        <h3 id="三、模糊查询"   >
          <a href="#三、模糊查询" class="heading-link"><i class="fas fa-link"></i></a>三、模糊查询</h3>
      <pre><code>    like
    between and
    in
    is null</code></pre>
<p>*/</p>

        <h3 id="示例一、按条件表达式筛选"   >
          <a href="#示例一、按条件表达式筛选" class="heading-link"><i class="fas fa-link"></i></a>示例一、按条件表达式筛选</h3>
      <p>案例1：查询工资&gt;12000的员工信息</p>
<figure class="highlight sql"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> </span><br><span class="line">	<span class="operator">*</span></span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line">	employees</span><br><span class="line"><span class="keyword">WHERE</span></span><br><span class="line">	salary<span class="operator">&gt;</span><span class="number">12000</span>;</span><br></pre></td></tr></table></div></figure>

<p>案例2：查询部门编号不等于90号的员工名和部门编号</p>
<figure class="highlight sql"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> </span><br><span class="line">	last_name,</span><br><span class="line">	department_id</span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line">	employees</span><br><span class="line"><span class="keyword">WHERE</span></span><br><span class="line">	department_id<span class="operator">&lt;&gt;</span><span class="number">90</span>;</span><br></pre></td></tr></table></div></figure>

        <h3 id="二、按逻辑表达式筛选-1"   >
          <a href="#二、按逻辑表达式筛选-1" class="heading-link"><i class="fas fa-link"></i></a>二、按逻辑表达式筛选</h3>
      <p>#案例1：查询工资z在10000到20000之间的员工名、工资以及奖金</p>
<figure class="highlight sql"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span></span><br><span class="line">	last_name,</span><br><span class="line">	salary,</span><br><span class="line">	commission_pct</span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line">	employees</span><br><span class="line"><span class="keyword">WHERE</span></span><br><span class="line">	salary<span class="operator">&gt;=</span><span class="number">10000</span> <span class="keyword">AND</span> salary<span class="operator">&lt;=</span><span class="number">20000</span>;</span><br></pre></td></tr></table></div></figure>
<p>#案例2：查询部门编号不是在90到110之间，或者工资高于15000的员工信息</p>
<figure class="highlight sql"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span></span><br><span class="line">	<span class="operator">*</span></span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line">	employees</span><br><span class="line"><span class="keyword">WHERE</span></span><br><span class="line">	<span class="keyword">NOT</span>(department_id<span class="operator">&gt;=</span><span class="number">90</span> <span class="keyword">AND</span>  department_id<span class="operator">&lt;=</span><span class="number">110</span>) <span class="keyword">OR</span> salary<span class="operator">&gt;</span><span class="number">15000</span>;</span><br></pre></td></tr></table></div></figure>

        <h3 id="三、模糊查询-1"   >
          <a href="#三、模糊查询-1" class="heading-link"><i class="fas fa-link"></i></a>三、模糊查询</h3>
      <p>/*<br>like</p>
<p>between and<br><br>in<br>is null | is not null</p>
<p><em>/<br>#1.like<br>/</em><br>特点：<br>①一般和通配符搭配使用<br>    通配符：<br>    % 任意多个字符,包含0个字符<br>    _ 任意单个字符<br>*、</p>
<p>#案例1：查询员工名中包含字符a的员工信息</p>
<figure class="highlight sql"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> </span><br><span class="line">	<span class="operator">*</span></span><br><span class="line"><span class="keyword">from</span></span><br><span class="line">	employees</span><br><span class="line"><span class="keyword">where</span></span><br><span class="line">	last_name <span class="keyword">like</span> <span class="string">&#x27;%a%&#x27;</span>;   #abc</span><br></pre></td></tr></table></div></figure>
<br>
#案例2：查询员工名中第三个字符为e，第五个字符为a的员工名和工资<br>
<figure class="highlight sql"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span></span><br><span class="line">	last_name,</span><br><span class="line">	salary</span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line">	employees</span><br><span class="line"><span class="keyword">WHERE</span></span><br><span class="line">	last_name <span class="keyword">LIKE</span> <span class="string">&#x27;__n_l%&#x27;</span>;</span><br></pre></td></tr></table></div></figure>
#案例3：查询员工名中第二个字符为_的员工名
<figure class="highlight sql"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span></span><br><span class="line">	last_name</span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line">	employees</span><br><span class="line"><span class="keyword">WHERE</span></span><br><span class="line">	last_name <span class="keyword">LIKE</span> <span class="string">&#x27;_$_%&#x27;</span> <span class="keyword">ESCAPE</span> <span class="string">&#x27;$&#x27;</span>;</span><br><span class="line">    # <span class="keyword">ESCAPE</span> <span class="string">&#x27;$&#x27;</span>,<span class="string">&#x27;$&#x27;</span>后面的<span class="string">&#x27;_&#x27;</span>当做普通字符<span class="string">&#x27;_&#x27;</span>使用</span><br></pre></td></tr></table></div></figure>
#2.between and<br>
/*
①使用between and 可以提高语句的简洁度<br>
②包含临界值
③两个临界值不要调换顺序

<p>*/</p>
<p>#案例1：查询员工编号在100到120之间的员工信息</p>
<figure class="highlight sql"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span></span><br><span class="line">	<span class="operator">*</span></span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line">	employees</span><br><span class="line"><span class="keyword">WHERE</span></span><br><span class="line">	employee_id <span class="operator">&gt;=</span> <span class="number">120</span> <span class="keyword">AND</span> employee_id<span class="operator">&lt;=</span><span class="number">100</span>;</span><br></pre></td></tr></table></div></figure>
<p>#———————-</p>
<figure class="highlight sql"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span></span><br><span class="line">	<span class="operator">*</span></span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line">	employees</span><br><span class="line"><span class="keyword">WHERE</span></span><br><span class="line">	employee_id <span class="keyword">BETWEEN</span> <span class="number">120</span> <span class="keyword">AND</span> <span class="number">100</span>;</span><br></pre></td></tr></table></div></figure>
<p>#3.in<br><br>/*<br>含义：判断某字段的值是否属于in列表中的某一项<br>特点：<br>    ①使用in提高语句简洁度<br>    ②in列表的值类型必须一致或兼容<br>    ③in列表中不支持通配符</p>
<p>*/<br>#案例：查询员工的工种编号是 IT_PROG、AD_VP、AD_PRES中的一个员工名和工种编号</p>
<figure class="highlight sql"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span></span><br><span class="line">	last_name,</span><br><span class="line">	job_id</span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line">	employees</span><br><span class="line"><span class="keyword">WHERE</span></span><br><span class="line">	job_id <span class="operator">=</span> <span class="string">&#x27;IT_PROT&#x27;</span> <span class="keyword">OR</span> job_id <span class="operator">=</span> <span class="string">&#x27;AD_VP&#x27;</span> <span class="keyword">OR</span> JOB_ID <span class="operator">=</span><span class="string">&#x27;AD_PRES&#x27;</span>;</span><br></pre></td></tr></table></div></figure>
<p>#——————</p>
<figure class="highlight sql"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span></span><br><span class="line">	last_name,</span><br><span class="line">	job_id</span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line">	employees</span><br><span class="line"><span class="keyword">WHERE</span></span><br><span class="line">	job_id <span class="keyword">IN</span>( <span class="string">&#x27;IT_PROT&#x27;</span> ,<span class="string">&#x27;AD_VP&#x27;</span>,<span class="string">&#x27;AD_PRES&#x27;</span>);</span><br></pre></td></tr></table></div></figure>
<p>#4、is null<br><br>/*<br>=或&lt;&gt;不能用于判断null值<br>is null或is not null 可以判断null值<br>*/</p>
<p>#案例1：查询没有奖金的员工名和奖金率</p>
<figure class="highlight sql"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span></span><br><span class="line">	last_name,</span><br><span class="line">	commission_pct</span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line">	employees</span><br><span class="line"><span class="keyword">WHERE</span></span><br><span class="line">	commission_pct <span class="keyword">IS</span> <span class="keyword">NULL</span>;</span><br></pre></td></tr></table></div></figure>
<p>#案例1：查询有奖金的员工名和奖金率</p>
<figure class="highlight sql"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span></span><br><span class="line">	last_name,</span><br><span class="line">	commission_pct</span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line">	employees</span><br><span class="line"><span class="keyword">WHERE</span></span><br><span class="line">	commission_pct <span class="keyword">IS</span> <span class="keyword">NOT</span> <span class="keyword">NULL</span>;</span><br></pre></td></tr></table></div></figure>

<p>#安全等于  &lt;=&gt;</p>
<p>#案例1：查询没有奖金的员工名和奖金率</p>
<figure class="highlight sql"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span></span><br><span class="line">	last_name,</span><br><span class="line">	commission_pct</span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line">	employees</span><br><span class="line"><span class="keyword">WHERE</span></span><br><span class="line">	commission_pct <span class="operator">&lt;=&gt;</span><span class="keyword">NULL</span>;</span><br></pre></td></tr></table></div></figure>

<p>#案例2：查询工资为12000的员工信息</p>
<figure class="highlight sql"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span></span><br><span class="line">	last_name,</span><br><span class="line">	salary</span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line">	employees</span><br><span class="line"></span><br><span class="line"><span class="keyword">WHERE</span> </span><br><span class="line">	salary <span class="operator">&lt;=&gt;</span> <span class="number">12000</span>;</span><br></pre></td></tr></table></div></figure>

<p>#is null pk &lt;=&gt;</p>
<p>IS NULL:仅仅可以判断NULL值，可读性较高，建议使用<br><br>&lt;=&gt;    :既可以判断NULL值，又可以判断普通的数值，可读性较低</p>
</div></div></article><article class="postlist-item post"><header class="post-header"><h1 class="post-title"><a class="post-title__link" href="/2021/01/24/jsp_01/">JSP 学习</a></h1><div class="post-meta"><span class="post-meta-item post-meta-item--createtime"><span class="post-meta-item__icon"><i class="far fa-calendar-plus"></i></span><span class="post-meta-item__info">Created</span><span class="post-meta-item__value">2021-01-24</span></span><span class="post-meta-item post-meta-item--updatetime"><span class="post-meta-item__icon"><i class="far fa-calendar-check"></i></span><span class="post-meta-item__info">Updated</span><span class="post-meta-item__value">2021-01-25</span></span></div></header><div class="post-body"><div class="post-excerpt">
        <h2 id="代码脚本"   >
          <a href="#代码脚本" class="heading-link"><i class="fas fa-link"></i></a>代码脚本</h2>
      <p>代码脚本的格式是：<br><code>    &lt;%     java 语句     %&gt;</code><br>代码脚本的作用是： 可以在 jsp 页面中， 编写我们自己需要的功能（写的是 java 语句） 。<br>代码脚本的特点是：<br>1、 代码脚本翻译之后都在_jspService 方法中<br>2、 代码脚本由于翻译到_jspService()方法中， 所以在_jspService()方法中的现有对象都可以直接使用。<br>3、 还可以由多个代码脚本块组合完成一个完整的 java 语句。<br>4、 代码脚本还可以和表达式脚本一起组合使用， 在 jsp 页面上输出数据<br>翻译之后的对比：<br><img src="/images/jsp/jsp1_duibi.png"> </p>

        <h2 id="JSP-九大内置对象"   >
          <a href="#JSP-九大内置对象" class="heading-link"><i class="fas fa-link"></i></a>JSP 九大内置对象</h2>
      <p>jsp 中的内置对象， 是指 Tomcat 在翻译 jsp 页面成为 Servlet 源代码后， 内部提供的九大对象， 叫内置对象<br><img src="/images/jsp/jsp1_duixiang.png"> </p>

        <h2 id="jsp-四大域对象"   >
          <a href="#jsp-四大域对象" class="heading-link"><i class="fas fa-link"></i></a>jsp 四大域对象</h2>
      <style>
table th:nth-of-type(1){
width: 18%;
}
table th:nth-of-type(2){
width: 30%
;
}
table th:nth-of-type(3){
width: 60%;
}
</style>
<p>四个域对象分别是：</p>
<div class="table-container"><table>
<thead>
<tr>
<th align="center">对象</th>
<th align="center">实现类</th>
<th align="center">页面有效范围</th>
</tr>
</thead>
<tbody><tr>
<td align="center">pageContext</td>
<td align="center">PageContextImpl 类</td>
<td align="center">当前 jsp 页面范围内有效</td>
</tr>
<tr>
<td align="center">request</td>
<td align="center">HttpServletRequest 类</td>
<td align="center">一次请求内有效</td>
</tr>
<tr>
<td align="center">session</td>
<td align="center">HttpSession 类一个会话范围内有效</td>
<td align="center">（打开浏览器访问服务器， 直到关闭浏览器）</td>
</tr>
<tr>
<td align="center">application</td>
<td align="center">ServletContext 类</td>
<td align="center">整个 web 工程范围内都有效（只要 web 工程不停止， 数据都在）</td>
</tr>
</tbody></table></div>

        <h3 id="域对象"   >
          <a href="#域对象" class="heading-link"><i class="fas fa-link"></i></a>域对象</h3>
      <p>域对象是可以像Map一样存取数据的对象。<br>四个域对象功能一样。不同的是它们对数据的存取范围。<br>虽然四个域对象都可以存取数据。 在使用上它们是有优先顺序的。<br>四个域在使用的时候， 优先顺序分别是， 他们从小到大的范围的顺序。<br>pageContext —&gt; request —&gt;session —&gt; application</p>

        <h3 id="jsp中的out输出和response-getWriter-输出的区别"   >
          <a href="#jsp中的out输出和response-getWriter-输出的区别" class="heading-link"><i class="fas fa-link"></i></a>jsp中的out输出和response.getWriter 输出的区别</h3>
      <p>response 中表示响应， 我们经常用于设置返回给客户端的内容（输出）<br>out 也是给用户做输出使用的。<br><img src="/images/jsp/out_print.png"> </p>
<p>由于 jsp 翻译之后， 底层源代码都是使用 out 来进行输出， 所以一般情况下。 我们在 jsp 页面中统一使用 out 来进行输出。 避免打乱页面输出内容的顺序。<br>out.write() 输出字符串没有问题<br>out.print() 输出任意数据都没有问题（都转换成为字符串后调用的 write 输出）</p>

        <h3 id="jsp-的常用标签"   >
          <a href="#jsp-的常用标签" class="heading-link"><i class="fas fa-link"></i></a>jsp 的常用标签</h3>
      
        <h4 id="a-jsp-静态包含"   >
          <a href="#a-jsp-静态包含" class="heading-link"><i class="fas fa-link"></i></a>a)jsp 静态包含</h4>
      <p>示例说明：<br>&lt;%–<br>&lt;%@ include file=””%&gt; 就是静态包含<br>file 属性指定你要包含的 jsp 页面的路径<br>地址中第一个斜杠 / 表示为 <a href="http://ip:port/工程路径/">http://ip:port/工程路径/</a> 映射到代码的 web 目录<br>静态包含的特点：<br>1、 静态包含不会翻译被包含的 jsp 页面。<br>2、 静态包含其实是把被包含的 jsp 页面的代码拷贝到包含的位置执行输出。<br>–%&gt;</p>
<figure class="highlight plain"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;%@ include file&#x3D;&quot;&#x2F;include&#x2F;footer.jsp&quot;%&gt;</span><br></pre></td></tr></table></div></figure>

        <h4 id="b-jsp-动态包含"   >
          <a href="#b-jsp-动态包含" class="heading-link"><i class="fas fa-link"></i></a>b)jsp 动态包含</h4>
      <p>示例说明：<br>&lt;%–<br><code>&lt;jsp:include page=&quot;&quot;&gt;&lt;/jsp:include&gt;</code><br> 这是动态包含page 属性是指定你要包含的 jsp 页面的路径<br>动态包含也可以像静态包含一样。 把被包含的内容执行输出到包含位置<br>动态包含的特点：<br>1、 动态包含会把包含的 jsp 页面也翻译成为 java 代码<br>2、 动态包含底层代码使用如下代码去调用被包含的 jsp 页面执行输出。<br>JspRuntimeLibrary.include(request, response, “/include/footer.jsp”, out, false);<br>3、 动态包含， 还可以传递参数<br>–%&gt;</p>
<figure class="highlight"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&lt;jsp:include page=<span class="string">&quot;/include/footer.jsp&quot;</span>&gt;</span><br><span class="line"> &lt;jsp:param name=<span class="string">&quot;username&quot;</span> value=<span class="string">&quot;bbj&quot;</span>/&gt;</span><br><span class="line"> &lt;jsp:param name=<span class="string">&quot;password&quot;</span> value=<span class="string">&quot;root&quot;</span>/&gt;</span><br><span class="line">&lt;/jsp:include&gt;</span><br></pre></td></tr></table></div></figure>

        <h5 id="动态包含的底层原理："   >
          <a href="#动态包含的底层原理：" class="heading-link"><i class="fas fa-link"></i></a>动态包含的底层原理：</h5>
      <p><img src="/images/jsp/dymicbaohan.png"> </p>

        <h4 id="jsp-标签-转发"   >
          <a href="#jsp-标签-转发" class="heading-link"><i class="fas fa-link"></i></a>jsp 标签-转发</h4>
      <p>示例说明：<br>&lt;%–<br><code>&lt;jsp:forward page=&quot;&quot;&gt;&lt;/jsp:forward&gt;</code> 是请求转发标签， 它的功能就是请求转发<br>page 属性设置请求转发的路径<br>–%&gt;<br><code>&lt;jsp:forward page=&quot;/scope2.jsp&quot;&gt;&lt;/jsp:forward&gt;</code></p>

        <h2 id="什么是-Listener-监听器？"   >
          <a href="#什么是-Listener-监听器？" class="heading-link"><i class="fas fa-link"></i></a>什么是 Listener 监听器？</h2>
      <p>1、 Listener 监听器它是 JavaWeb 的三大组件之一。 JavaWeb 的三大组件分别是： Servlet 程序、 Filter 过滤器、 Listener 监听器。<br>2、 Listener 它是 JavaEE 的规范， 就是接口<br>3、 监听器的作用是， 监听某种事物的变化。 然后通过回调函数， 反馈给客户（程序） 去做一些相应的处理。<br>1.1、 ServletContextListener 监听器<br>ServletContextListener 它可以监听 ServletContext 对象的创建和销毁。<br>ServletContext 对象在 web 工程启动的时候创建， 在 web 工程停止的时候销毁。<br>监听到创建和销毁之后都会分别调用 ServletContextListener 监听器的方法反馈<br>如何使用 ServletContextListener 监听器监听 ServletContext 对象。<br>使用步骤如下：<br>    1、 编写一个类去实现 ServletContextListener<br>    2、 实现其两个回调方法<br>    3、 到 web.xml 中去配置监听器<br>监听器实现类：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyServletContextListenerImpl</span> <span class="keyword">implements</span> <span class="title">ServletContextListener</span> </span>&#123;</span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">contextInitialized</span><span class="params">(ServletContextEvent sce)</span> </span>&#123;</span><br><span class="line">	System.out.println(<span class="string">&quot;ServletContext 对象被创建了&quot;</span>);</span><br><span class="line">	&#125; </span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">contextDestroyed</span><span class="params">(ServletContextEvent sce)</span> </span>&#123;</span><br><span class="line">	System.out.println(<span class="string">&quot;ServletContext 对象被销毁了&quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line">&#125; </span><br></pre></td></tr></table></div></figure>
<p>web.xml 中的配置：</p>
<figure class="highlight xml"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--配置监听器--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">listener</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">listener-class</span>&gt;</span>com.atguigu.listener.MyServletContextListenerImpl<span class="tag">&lt;/<span class="name">listener-class</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">listener</span>&gt;</span></span><br></pre></td></tr></table></div></figure></div></div></article><article class="postlist-item post"><header class="post-header"><h1 class="post-title"><a class="post-title__link" href="/2021/01/23/hello-world/">Hello World</a></h1><div class="post-meta"><span class="post-meta-item post-meta-item--createtime"><span class="post-meta-item__icon"><i class="far fa-calendar-plus"></i></span><span class="post-meta-item__info">Created</span><span class="post-meta-item__value">2021-01-23</span></span><span class="post-meta-item post-meta-item--updatetime"><span class="post-meta-item__icon"><i class="far fa-calendar-check"></i></span><span class="post-meta-item__info">Updated</span><span class="post-meta-item__value">2021-01-24</span></span></div></header><div class="post-body"><div class="post-excerpt"><p>Welcome to <span class="exturl"><a class="exturl__link"   target="_blank" rel="noopener" href="https://hexo.io/" >Hexo</a><span class="exturl__icon"><i class="fas fa-external-link-alt"></i></span></span>! This is your very first post. Check <span class="exturl"><a class="exturl__link"   target="_blank" rel="noopener" href="https://hexo.io/docs/" >documentation</a><span class="exturl__icon"><i class="fas fa-external-link-alt"></i></span></span> for more info. If you get any problems when using Hexo, you can find the answer in <span class="exturl"><a class="exturl__link"   target="_blank" rel="noopener" href="https://hexo.io/docs/troubleshooting.html" >troubleshooting</a><span class="exturl__icon"><i class="fas fa-external-link-alt"></i></span></span> or you can ask me on <span class="exturl"><a class="exturl__link"   target="_blank" rel="noopener" href="https://github.com/hexojs/hexo/issues" >GitHub</a><span class="exturl__icon"><i class="fas fa-external-link-alt"></i></span></span>.</p>

        <h2 id="Quick-Start"   >
          <a href="#Quick-Start" class="heading-link"><i class="fas fa-link"></i></a>Quick Start</h2>
      
        <h3 id="Create-a-new-post"   >
          <a href="#Create-a-new-post" class="heading-link"><i class="fas fa-link"></i></a>Create a new post</h3>
      <figure class="highlight bash"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo new <span class="string">&quot;My New Post&quot;</span></span><br></pre></td></tr></table></div></figure>
<p>More info: <span class="exturl"><a class="exturl__link"   target="_blank" rel="noopener" href="https://hexo.io/docs/writing.html" >Writing</a><span class="exturl__icon"><i class="fas fa-external-link-alt"></i></span></span></p>

        <h3 id="Run-server"   >
          <a href="#Run-server" class="heading-link"><i class="fas fa-link"></i></a>Run server</h3>
      <figure class="highlight bash"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo server</span><br></pre></td></tr></table></div></figure>
<p>More info: <span class="exturl"><a class="exturl__link"   target="_blank" rel="noopener" href="https://hexo.io/docs/server.html" >Server</a><span class="exturl__icon"><i class="fas fa-external-link-alt"></i></span></span></p>

        <h3 id="Generate-static-files"   >
          <a href="#Generate-static-files" class="heading-link"><i class="fas fa-link"></i></a>Generate static files</h3>
      <figure class="highlight bash"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo generate</span><br></pre></td></tr></table></div></figure>
<p>More info: <span class="exturl"><a class="exturl__link"   target="_blank" rel="noopener" href="https://hexo.io/docs/generating.html" >Generating</a><span class="exturl__icon"><i class="fas fa-external-link-alt"></i></span></span></p>

        <h3 id="Deploy-to-remote-sites"   >
          <a href="#Deploy-to-remote-sites" class="heading-link"><i class="fas fa-link"></i></a>Deploy to remote sites</h3>
      <figure class="highlight bash"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo deploy</span><br></pre></td></tr></table></div></figure>
<p>More info: <span class="exturl"><a class="exturl__link"   target="_blank" rel="noopener" href="https://hexo.io/docs/one-command-deployment.html" >Deployment</a><span class="exturl__icon"><i class="fas fa-external-link-alt"></i></span></span></p>
</div></div></article></section><nav class="paginator"><div class="paginator-inner"><span class="page-number current">1</span></div></nav></div></div><div class="sidebar-wrap" id="sidebar-wrap"><aside class="sidebar" id="sidebar"><section class="sidebar-toc hide"></section><!-- ov = overview--><section class="sidebar-ov"><div class="sidebar-ov-author"><div class="sidebar-ov-author__avatar"><img class="sidebar-ov-author__avatar_img" src="/images/touxiang2.jpg" alt="avatar"></div><p class="sidebar-ov-author__text">Storms make trees take deeper roots</p></div><div class="sidebar-ov-social"><a class="sidebar-ov-social-item" href="https://github.com/Gityang2016" target="_blank" rel="noopener" data-popover="Github" data-popover-pos="up"><span class="sidebar-ov-social-item__icon"><i class="fab fa-github"></i></span></a></div><div class="sidebar-ov-state"><a class="sidebar-ov-state-item sidebar-ov-state-item--posts" href="/archives/"><div class="sidebar-ov-state-item__count">6</div><div class="sidebar-ov-state-item__name">Archives</div></a><a class="sidebar-ov-state-item sidebar-ov-state-item--categories" href="/categories/"><div class="sidebar-ov-state-item__count">8</div><div class="sidebar-ov-state-item__name">Categories</div></a><a class="sidebar-ov-state-item sidebar-ov-state-item--tags" href="/tags/"><div class="sidebar-ov-state-item__count">5</div><div class="sidebar-ov-state-item__name">Tags</div></a></div><div class="sidebar-ov-cc"><a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.en" target="_blank" rel="noopener" data-popover="Creative Commons" data-popover-pos="up"><img src="/images/cc-by-nc-sa.svg"></a></div></section></aside></div><div class="clearfix"></div></div></main><footer class="footer" id="footer"><div class="footer-inner"><div><span>Copyright © 2021</span><span class="footer__icon"><i class="fas fa-heart"></i></span><span>Gityang2016</span></div><div><span>Powered by <a href="http://hexo.io/" title="Hexo" target="_blank" rel="noopener">Hexo</a></span><span> v5.3.0</span><span class="footer__devider">|</span><span>Theme - <a href="https://github.com/liuyib/hexo-theme-stun/" title="Stun" target="_blank" rel="noopener">Stun</a></span><span> v2.6.1</span></div></div></footer><div class="loading-bar" id="loading-bar"><div class="loading-bar__progress"></div></div><div class="back2top" id="back2top"><span class="back2top__icon"><i class="fas fa-rocket"></i></span></div></div><script src="https://cdn.jsdelivr.net/npm/jquery@v3.4.1/dist/jquery.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-animate@1.5.2/velocity.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-animate@1.5.2/velocity.ui.min.js"></script><script src="/js/utils.js?v=2.6.1"></script><script src="/js/stun-boot.js?v=2.6.1"></script><script src="/js/scroll.js?v=2.6.1"></script><script src="/js/header.js?v=2.6.1"></script><script src="/js/sidebar.js?v=2.6.1"></script></body></html>